package com.xy.mainp.home.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;

import com.xy.debug.LogUtils;
import com.xy.mainp.R;
import com.xy.mainp.base.BasePFragment;
import com.xy.mainp.databinding.FragmentAndroidBinding;
import com.xy.mainp.home.fragment.widget.HomeAdapter;
import com.xy.mainp.model.home.android.HomeEntity;
import com.xy.mainp.model.home.android.HomeResultEntity;
import com.xy.mainp.utils.NetRequestUtils;
import com.xy.net.OkHttpUtils;

import java.util.List;

import okhttp3.Request;

/**
 * Created by John on 2016/11/7.
 */

public abstract class HomeBaseFragment extends BasePFragment implements SwipeRefreshLayout.OnRefreshListener {

    private FragmentAndroidBinding binding;
    private HomeAdapter adapter;
    private List<HomeResultEntity> entity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_android, null, false);
        setContentView(binding.getRoot());
        initView();
    }

    private void initView() {
        binding.srlAndroid.setOnRefreshListener(this);
        //加载颜色是循环播放的，只要没有完成刷新就会一直循环，color1>color2>color3>color4
        binding.srlAndroid.setColorSchemeResources(R.color.blue_60, R.color.yellow_f2);

        binding.rvAndroid.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new HomeAdapter();
        binding.rvAndroid.setAdapter(adapter);
    }

    protected void loadData(String type,String count,String page) {
        NetRequestUtils.getInstance(getContext()).getHomeData(type, count, page, new OkHttpUtils.ResultCallBack<HomeEntity>() {
            @Override
            public void onResult(HomeEntity result) {
                binding.srlAndroid.setRefreshing(false);
                if (!result.getError()) {
                    entity = result.getResults();
                    adapter.setData(entity);
                }
            }

            @Override
            public void onError(Request request, Exception e) {
                LogUtils.w(e.getMessage());
            }
        });
    }


}
